﻿package pl.minim.unidefCMS.objects {
	import flash.debugger.enterDebugger;
	import com.googlecode.milib.base.MLBaseClass;
	
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	
	import pl.minim.unidefCMS.UnidefManager;	

	/**
	 * @author Marek Brun
	 */
	public class AbstractUnidefObject extends MLBaseClass {
		
		public var id:String;
		public var parent:AbstractUnidefObject;
		public var manager:UnidefManager;

		public function AbstractUnidefObject() {
			
		}
		
		public function getXMLDefParentsPatch():Array {
			if(parent){
				return parent.getXMLDefParentsPatch().concat([this]);
			}
			return [this];
		}
		
		/*abstract*/ public function getTypeName():String {
			return 'none';
		}
		
		/*abstract*/ public function getDataAsTextPreview():String {
			return '-';
		}
		
		/*abstract*/ public function toXML():XML {
			return null;
		}
		
		/*abstract*/ public function exportToRawData():* {
			return null;
		}
		
		/*abstract*/ public function reactToPredefinedObjectDelete(deletedObject:UnidefClass):void { }
		
		static public function createByXML(xmlDef:XML, xmlData:XML, manager:UnidefManager):AbstractUnidefObject {
			var unidefObj:AbstractUnidefObject;
			var i:uint;
			var loop:XML;
			var desc:String;
			
			switch(xmlDef.@type.toString()){
				case "class":
					if(xmlDef.@clazz!=undefined){
						var id:String=xmlDef.@id.toString();
						xmlDef=manager.getXMLClassByID(xmlDef.@clazz.toString());
						xmlDef.@id=id;
					}
					var defClass:UnidefClass=new UnidefClass(xmlDef.@id.toString(), xmlDef.@className.toString()), item:AbstractUnidefObject;
					for(i=0;i<xmlDef.item.length();i++){
						loop=xmlDef.item[i];
						if(loop.attribute("descr") && loop.@descr.toString().length){
							desc=loop.@descr.toString();
							enterDebugger();
						}
						if(xmlData){
							if(!xmlData.i[i]){
								throw new Error('There\'s no data for class '+defClass.clazzID+' (id:'+xmlDef.@id.toString()+') at index '+i+', id:'+i+', type:'+loop.@type.toString());
							}
							defClass.addObject(AbstractUnidefObject.createByXML(loop, xmlData.i[i], manager), desc);
						}else{
							item=AbstractUnidefObject.createByXML(loop, null, manager);
							if(item is UnidefClass){ item.id=loop.@id.toString(); }
							defClass.addObject(item, desc);
						}
					}
					unidefObj=defClass;
					break;
				case "list":
					var defList:UnidefList=new UnidefList(xmlDef.@clazz.toString());
					var clazz:XML=manager.getXMLClassByID(defList.clazz);
					var listItem:AbstractUnidefObject;
					if(xmlData && xmlData.i){
						for(i=0;i<xmlData.i.length();i++){
							loop=xmlData.i[i];
							listItem=AbstractUnidefObject.createByXML(clazz, loop, manager);
							listItem.id=loop.@id.toString();
							defList.addObject(listItem);
						}
					}
					unidefObj=defList;
					break;
				case "predefined":
					var defPredefined:UnidefPredefined=new UnidefPredefined(xmlDef.@clazz.toString(), manager);
					if(xmlData && xmlData.toString().length){
						defPredefined.setChoosedObjByID(xmlData.toString());
					}
					unidefObj=defPredefined;
					break;
				case "predefinedList":
					var defPredefinedList:UnidefPredefinedList=new UnidefPredefinedList(xmlDef.@clazz.toString(), manager);
					if(xmlData && xmlData.toString().length){
						var ids:Array=xmlData.toString().split(',');
						for(i=0;i<ids.length;i++){
							if(!ids[i].length){ continue; }
							defPredefinedList.addSelectedObjByID(ids[i]);
						}
					}
					unidefObj=defPredefinedList;
					break;
				case "txtBig":
					var defTextBig:AbstractUnidefText=new UnidefTextBig();
					if(xmlData){
						defTextBig.text=xmlData.toString();
					}
					unidefObj=defTextBig;
					break;
				case "txtSmall":
					var defTextSmall:AbstractUnidefText=new UnidefTextSmall();
					if(xmlData){
						defTextSmall.text=xmlData.toString();
					}
					unidefObj=defTextSmall;
					break;
				case "html":
					var defHTML:UnidefHTML=new UnidefHTML();
					if(xmlData){
						defHTML.text=xmlData.toString();
					}
					unidefObj=defHTML;
					break;
				case "url":
					var defURL:UnidefURL=new UnidefURL();
					if(xmlData){
						defURL.url=new URLRequest(xmlData.toString());
					}
					unidefObj=defURL;
					break;
				case "bool":
					var defBool:UnidefBoolean=new UnidefBoolean();
					if(xmlData){
						defBool.value=Boolean(Number(xmlData.toString()));
					}
					unidefObj=defBool;
					break;
				case "date":
					var defDate:UnidefDate=new UnidefDate();
					if(xmlData){
						defDate.date=new Date(Number(xmlData.toString()));
					}
					unidefObj=defDate;
					break;
				case "dateScope":
					var defDateScope:UnidefDateScope=new UnidefDateScope();
					if(xmlData){
						defDateScope.date=new Date(Number(xmlData.toString().split('|')[0]));
						defDateScope.date2=new Date(Number(xmlData.toString().split('|')[1]));
					}
					unidefObj=defDateScope;
					break;
				case "num":
					var defNum:UnidefNumber=new UnidefNumber();
					if(xmlDef.@min!=undefined){ defNum.min=Number(xmlDef.@min.toString()); defNum.value=defNum.min; }
					if(xmlDef.@max!=undefined){ defNum.max=Number(xmlDef.@max.toString()); }
					defNum.type=xmlDef.@num.toString()=='int' ?  UnidefNumber.TYPE_INT : UnidefNumber.TYPE_NUMBER;
					if(xmlData){
						defNum.value=Number(xmlData.toString());
					}
					unidefObj=defNum;
					break;
				case "numScope":
					var defNumScope:UnidefNumberScope=new UnidefNumberScope();
					if(xmlDef.@min!=undefined){ defNumScope.min=Number(xmlDef.@min.toString()); defNumScope.value2=defNumScope.value=defNumScope.min; }
					if(xmlDef.@max!=undefined){ defNumScope.max=Number(xmlDef.@max.toString()); defNumScope.value2=defNumScope.max; }
					defNumScope.type=xmlDef.@num.toString()=='int' ?  UnidefNumber.TYPE_INT : UnidefNumber.TYPE_NUMBER;
					unidefObj=defNumScope;
					if(xmlData){
						defNumScope.value=Number(xmlData.toString().split('|')[0]);
						defNumScope.value2=Number(xmlData.toString().split('|')[1]);
					}
					break;
				case "file":
					var defFile:UnidefFile=new UnidefFile();
					defFile.filter=new FileFilter(xmlDef.@desc.toString(), xmlDef.@extemsion.toString());
					if(xmlData){
						defFile.url=new URLRequest(xmlData.toString());
					}
					unidefObj=defFile;
					break;
				default:
					throw new Error('Unsupported unidef type "'+xmlDef.@type.toString()+'"');
			}
			
			unidefObj.manager=manager;
			if(unidefObj is UnidefClass){
				if(xmlData){
					unidefObj.id=xmlData.@id.toString();
				}
			}else{
				unidefObj.id=xmlDef.@id.toString();
			}
			
			return unidefObj;
		}
		
	}
}
/*

<item type="class" id="clients">
	<item type="txt" id="name" editLines="1"/>
	<item type="url" id="link"/>
</item>

*/